//package com.ruoyi.mp.controller;
//
//import com.ruoyi.common.core.utils.poi.ExcelUtil;
//import com.ruoyi.common.core.web.controller.BaseController;
//import com.ruoyi.common.core.web.domain.AjaxResult;
//import com.ruoyi.common.core.web.page.TableDataInfo;
//import com.ruoyi.common.log.annotation.Log;
//import com.ruoyi.common.log.enums.BusinessType;
//import com.ruoyi.common.security.annotation.RequiresPermissions;
//import com.ruoyi.mp.api.domain.MpAccount;
//import com.ruoyi.mp.domain.MpKf;
//import com.ruoyi.mp.service.IMpAccountService;
//import com.ruoyi.mp.service.IMpKfService;
//import me.chanjar.weixin.common.error.WxErrorException;
//import me.chanjar.weixin.mp.api.WxMpService;
//import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfMsgList;
//import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionGetResult;
//import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionList;
//import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionWaitCaseList;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import javax.servlet.http.HttpServletResponse;
//import java.util.Date;
//import java.util.List;
//
///**
// * <h3>springcloudSaas</h3>
// * <p>发布能力</p>
// *
// * @author : 郎兴宇
// * @date : 2024-09-29 20:43
// **/
//@RestController
//@RequestMapping("/kfManage")
//public class MpKfManageController extends BaseController {
//    @Autowired
//    private WxMpService wxService;
//    @Autowired
//    private IMpAccountService iMpAccountService;
//    @Autowired
//    private IMpKfService iMpKfService;
//        /**
//     * 添加客服微信，此处微信号为公众号微信号
//     */
//    @PostMapping("/kfAccountAdd/{id}")
//    @RequiresPermissions("mp:kfManage:kfAccountAdd")
//    @Log(title = "添加客服", businessType = BusinessType.INSERT)
//    public AjaxResult kfAccountAdd(@PathVariable Long id,@RequestBody MpKf mpKf) throws WxErrorException {
//         MpAccount mpAccount = this.iMpAccountService.selectMpAccountById(id);
//        mpKf.setKfAccount(mpKf.getKfAccount()+"@"+mpAccount.getAccount());
//        mpKf.setAppId(mpAccount.getAppId());
//        return toAjax(iMpKfService.insertMpKf(mpKf));
//    }
//
//
//    /**
//     * 绑定客服微信，此微信号为用户微信微信号
//     */
//    @PostMapping("/kfInviteWorker/{id}")
//    @RequiresPermissions("mp:kfManage:kfInviteWorker")
//    @Log(title = "绑定客服微信", businessType = BusinessType.UPDATE)
//    public AjaxResult kfInviteWorker(@PathVariable Long id,@RequestBody MpKf mpKf) throws WxErrorException {
//        MpAccount mpAccount = this.iMpAccountService.selectMpAccountById(id);
//        return toAjax(iMpKfService.kfInviteWorker(mpKf));
//    }
//
//    /**
//     * 获取更新客服信息，此微信号为用户微信微信号
//     *
//     */
//    @GetMapping("/kfList/{id}")
//    @RequiresPermissions("mp:kfManage:kfList")
//    @Log(title = "获取更新客服信息", businessType = BusinessType.UPDATE)
//    public AjaxResult kfList(@PathVariable Long id) throws WxErrorException {
//        return toAjax(iMpKfService.getkflist(id));
//    }
//
//    /**
//     * 设置客服信息（即更新客服信息），此微信号为用户微信微信号
//     *
//     */
//    @PutMapping("/kfUpdate/{id}")
//    @RequiresPermissions("mp:kfManage:kfUpdate")
//    @Log(title = "设置客服信息（即更新客服信息）", businessType = BusinessType.UPDATE)
//    public AjaxResult kfUpdate(@PathVariable Long id,MpKf mpKf) throws WxErrorException {
//        mpKf.setAccountId(id);
//        return toAjax(iMpKfService.kfAccountUpdate(mpKf));
//    }
//
//
//    /**
//     * 设置客服头像，此微信号为用户微信微信号
//     *
//     */
//    @PutMapping("/kfUploadHeadImg/{id}")
//    @RequiresPermissions("mp:kfManage:kfUploadHeadImg")
//    @Log(title = "设置客服头像", businessType = BusinessType.UPDATE)
//    public AjaxResult kfUploadHeadImg(@PathVariable Long id,MpKf mpKf) throws WxErrorException {
//        mpKf.setAccountId(id);
//        return toAjax( iMpKfService.kfAccountUploadHeadImg(mpKf) ? 1 : 0) ;
//    }
//
//    /**
//     * 删除客服，此微信号为用户微信微信号
//     *
//     */
//    @DeleteMapping("/kfDelete/{id}")
//    @RequiresPermissions("mp:kfManage:kfDelete")
//    @Log(title = "删除客服", businessType = BusinessType.DELETE)
//    public AjaxResult kfAccountDel(@PathVariable Long id,Long accountId) throws WxErrorException {
//        MpAccount mpAccount = this.iMpAccountService.selectMpAccountById(accountId);
//        return toAjax( iMpKfService.kfAccountDel(mpAccount,id) ? 1 : 0 );
//
//    }
//
//    /**
//     *  创建会话，此微信号为用户微信微信号
//     *
//     */
//    @PostMapping("/kfSessionCreate/{id}")
//    @RequiresPermissions("mp:kfManage:kfSessionCreate")
//    @Log(title = "创建会话", businessType = BusinessType.INSERT)
//    public AjaxResult kfSessionCreate(@PathVariable Long id) throws WxErrorException {
//        MpAccount mpAccount = this.iMpAccountService.selectMpAccountById(id);
//        String kfAccount  =  "test@"+mpAccount.getAccount();
//        String openId = "o4iKz6gwJjCPrSMnwikbzLqIyj8U";
//        return toAjax(  wxService.switchoverTo(mpAccount.getAppId()).getKefuService().kfSessionCreate(openId,kfAccount) ? 1 : 0 );
//    }
//
//    /**
//     *  关闭会话，此微信号为用户微信微信号
//     *
//     */
//    @DeleteMapping("/kfSessionClose/{id}")
//    @RequiresPermissions("mp:kfManage:kfSessionCreate")
//    @Log(title = "关闭会话", businessType = BusinessType.DELETE)
//    public AjaxResult kfSessionClose(@PathVariable Long id) throws WxErrorException {
//        MpAccount mpAccount = this.iMpAccountService.selectMpAccountById(id);
//        String kfAccount  =  "test@"+mpAccount.getAccount();
//        String openId = "o4iKz6gwJjCPrSMnwikbzLqIyj8U";
//        return toAjax( wxService.switchoverTo(mpAccount.getAppId()).getKefuService().kfSessionClose(openId,kfAccount) ? 1 : 0 );
//    }
//
//
//
//    /**
//     *  获取客户的会话状态，此微信号为用户微信微信号
//     *
//     */
//    @GetMapping("/kfSessionGet/{id}")
//    @RequiresPermissions("mp:kfManage:kfSessionGet")
//    @Log(title = " 获取客户的会话状态", businessType = BusinessType.EXPORT)
//    public AjaxResult kfSessionGet(@PathVariable Long id) throws WxErrorException {
//        MpAccount mpAccount = this.iMpAccountService.selectMpAccountById(id);
//        String openId = "o4iKz6gwJjCPrSMnwikbzLqIyj8U";
//        WxMpKfSessionGetResult wxMpKfSessionGetResult    =    wxService.switchoverTo(mpAccount.getAppId()).getKefuService().kfSessionGet(openId);
//        return AjaxResult.success(wxMpKfSessionGetResult);
//    }
//
//
//    /**
//     *  获取客服的会话列表，此微信号为用户微信微信号
//     *
//     */
//    @GetMapping("/kfSessionList/{id}")
//    @RequiresPermissions("mp:kfManage:kfSessionList")
//    @Log(title = "获取客服的会话列表", businessType = BusinessType.EXPORT)
//    public AjaxResult kfSessionList(@PathVariable Long id) throws WxErrorException {
//        MpAccount mpAccount = this.iMpAccountService.selectMpAccountById(id);
//        String openId = "o4iKz6gwJjCPrSMnwikbzLqIyj8U";
//        WxMpKfSessionList wxMpKfSessionList    =    wxService.switchoverTo(mpAccount.getAppId()).getKefuService().kfSessionList(openId);
//        return AjaxResult.success(wxMpKfSessionList);
//    }
//
//    /**
//     *   获取未接入会话列表，此微信号为用户微信微信号
//     *
//     */
//    @GetMapping("/kfSessionGetWaitCase/{id}")
//    @RequiresPermissions("mp:kfManage:kfSessionGetWaitCase")
//    @Log(title = "获取未接入会话列表", businessType = BusinessType.EXPORT)
//    public AjaxResult kfSessionGetWaitCase(@PathVariable Long id) throws WxErrorException {
//        MpAccount mpAccount = this.iMpAccountService.selectMpAccountById(id);
//        WxMpKfSessionWaitCaseList wxMpKfSessionWaitCaseList    =    wxService.switchoverTo(mpAccount.getAppId()).getKefuService().kfSessionGetWaitCase();
//        return AjaxResult.success(wxMpKfSessionWaitCaseList);
//    }
//
//
//    /**
//     *   获取聊天记录（原始接口），此微信号为用户微信微信号
//     *
//     */
//    @GetMapping("/kfMsgList/{id}")
//    @RequiresPermissions("mp:kfManage:kfMsgList")
//    @Log(title = "获取聊天记录（优化接口）", businessType = BusinessType.EXPORT)
//    public AjaxResult kfMsgList(@PathVariable Long id) throws WxErrorException {
//        MpAccount mpAccount = this.iMpAccountService.selectMpAccountById(id);
//        Date startTime = new Date();
//        Date endTime = new Date();
//        WxMpKfMsgList wxMpKfMsgList  =  wxService.switchoverTo(mpAccount.getAppId()).getKefuService().kfMsgList(startTime,endTime);
//        return AjaxResult.success(wxMpKfMsgList);
//    }
//
//
//    /**
//     * 查询公众号客服列表
//     */
//    @RequiresPermissions("Mp:kf:list")
//    @GetMapping("/list")
//    public TableDataInfo list(MpKf mpKf)
//    {
//        startPage();
//        List<MpKf> list = iMpKfService.selectMpKfList(mpKf);
//        return getDataTable(list);
//    }
//
//    /**
//     * 导出公众号客服列表
//     */
//    @RequiresPermissions("Mp:kf:export")
//    @Log(title = "公众号客服", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, MpKf mpKf)
//    {
//        List<MpKf> list = mpKfService.selectMpKfList(mpKf);
//        ExcelUtil<MpKf> util = new ExcelUtil<MpKf>(MpKf.class);
//        util.exportExcel(response, list, "公众号客服数据");
//    }
//
//    /**
//     * 获取公众号客服详细信息
//     */
//    @RequiresPermissions("Mp:kf:query")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(mpKfService.selectMpKfById(id));
//    }
//
//    /**
//     * 新增公众号客服
//     */
//    @RequiresPermissions("Mp:kf:add")
//    @Log(title = "公众号客服", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody MpKf mpKf)
//    {
//        return toAjax(mpKfService.insertMpKf(mpKf));
//    }
//
//    /**
//     * 修改公众号客服
//     */
//    @RequiresPermissions("Mp:kf:edit")
//    @Log(title = "公众号客服", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody MpKf mpKf)
//    {
//        return toAjax(mpKfService.updateMpKf(mpKf));
//    }
//
//    /**
//     * 删除公众号客服
//     */
//    @RequiresPermissions("Mp:kf:remove")
//    @Log(title = "公众号客服", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        return toAjax(mpKfService.deleteMpKfByIds(ids));
//    }
//}
